package com.yuncheng.oaApi.busService;

import com.yuncheng.oaApi.vo.*;
import org.apache.commons.collections.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.oaApi.constant.*;
import com.yuncheng.oaApi.entity.*;
import com.yuncheng.oaApi.mapper.*;
import com.yuncheng.oaApi.service.*;
import com.yuncheng.vo.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Component(value = "MyLcFlowService")
public class MyLcFlowService {
    private static final Logger log = LoggerFactory.getLogger(MyLcFlowService.class);

    @Resource
    @Lazy
    private IOaYcSysUserService ycSysUserService;

    @Resource
    @Lazy
    private IOaSpcySqsxJobService spcySqsxJobService;

    @Resource
    @Lazy
    private OaDbrService oaDbrService;

    @Resource
    @Lazy
    private OaLgqsOaFlowConstants oaLgqsFlowConstant;

    @Resource
    @Lazy
    private OaLgqsFlowMapper oaLgqsFlowMapper;

    @Resource
    @Lazy
    private OaXzswspOaFlowConstants oaXzswspFlowConstant;

    @Resource
    @Lazy
    private OaXzswspFlowMapper oaXzswspFlowMapper;

    @Resource
    @Lazy
    private OaXcxxspOaFlowConstants oaXcxxspFlowConstant;

    @Resource
    @Lazy
    private OaXcxxspglFlowMapper oaXcxxspglFlowMapper;

    @Resource
    @Lazy
    private OaHysglOaFlowConstants oaHysglFlowConstant;

    @Resource
    @Lazy
    private OaHysglFlowMapper oaHysglFlowMapper;

    @Resource
    @Lazy
    private OaBgypsqglOaFlowConstants oaBgypsqglFlowConstant;

    @Resource
    @Lazy
    private OaBgypsqglFlowMapper oaBgypsqglFlowMapper;

    @Resource
    @Lazy
    private OajbzbglOaFlowConstants oajbzbglFlowConstant;

    @Resource
    @Lazy
    private OaJbzbFlowMapper oaJbzbFlowMapper;

    @Resource
    @Lazy
    private OaqxjOaFlowConstants oaqxjFlowConstant;

    @Resource
    @Lazy
    private OaQxjspFlowMapper oaQxjspFlowMapper;

    @Resource
    @Lazy
    private IOaSysFlowNodeService sysFlowNodeService;

    @Resource
    @Lazy
    private IOaProcessBusinessService oaProcessBusinessService;


    //离岗请示
    public Map myLgqsFlowDb(Page<OaFlowVo> page1, boolean onlyCurrentUser, CurrentUserVo user) {
        Map resultMap = new HashMap();

        Page<OaLgqsVo> page = new Page<>(1, 100);
        OaLgqsVo flowVo = new OaLgqsVo();
        flowVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        flowVo.setFlcmc(oaLgqsFlowConstant.getFlowName());
        IPage<OaLgqsVo> flowList = oaLgqsFlowMapper.flowJkPage(page, onlyCurrentUser, user, flowVo);

        Map<String, Long> FlowVo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(flowList.getRecords())) {
            FlowVo = flowList.getRecords().stream().collect(Collectors.groupingBy(
                    OaLgqsVo::getFhjmc, Collectors.counting()
            ));
        }


        List<NodeTjVo> lgqsMap = FlowVo.entrySet().stream().map(e -> new NodeTjVo(e.getKey(), e.getValue())).collect(Collectors.toList());

        resultMap.put("oaFlowNode", lgqsMap);

        return resultMap;
    }

    public Map myLgqsList(boolean onlyCurrentUser, CurrentUserVo user) {
        Map map = new HashMap<>();
        OaLgqsVo oaLgqsVo = new OaLgqsVo();
        oaLgqsVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        List<OaLgqsVo> lgqsFlowList = oaLgqsFlowMapper.flowJkList(onlyCurrentUser, user, oaLgqsVo);
        map.put("oaFlowNode", lgqsFlowList);
        return map;
    }

    //行政事务审批
    public Map myXzswspFlowDb(Page<OaFlowVo> page1, boolean onlyCurrentUser, CurrentUserVo user) {
        Map resultMap = new HashMap();

        Page<OaXzswspVo> page = new Page<>(1, 100);
        OaXzswspVo flowVo = new OaXzswspVo();
        flowVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        flowVo.setFlcmc(oaXzswspFlowConstant.getFlowName());
        IPage<OaXzswspVo> flowList = oaXzswspFlowMapper.flowJkPage(page, onlyCurrentUser, user, flowVo);

        Map<String, Long> FlowVo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(flowList.getRecords())) {
            FlowVo = flowList.getRecords().stream().collect(Collectors.groupingBy(
                    OaXzswspVo::getFhjmc, Collectors.counting()
            ));
        }

        List<NodeTjVo> xzswspMap = FlowVo.entrySet().stream().map(e -> new NodeTjVo(e.getKey(), e.getValue())).collect(Collectors.toList());
        resultMap.put("oaFlowNode", xzswspMap);

        return resultMap;
    }

    public Map myXzswspList(boolean onlyCurrentUser, CurrentUserVo user) {
        Map map = new HashMap<>();
        OaXzswspVo oaXzswspVo = new OaXzswspVo();
        oaXzswspVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        List<OaXzswspVo> xzswspFlowList = oaXzswspFlowMapper.flowJkList(onlyCurrentUser, user, oaXzswspVo);
        map.put("oaFlowNode", xzswspFlowList);
        return map;
    }

    //宣传宣传信息审批
    public Map myXcxxFlowDb(Page<OaFlowVo> page1, boolean onlyCurrentUser, CurrentUserVo user) {
        Map resultMap = new HashMap();

        Page<OaXcxxspglVo> page = new Page<>(1, 100);
        OaXcxxspglVo flowVo = new OaXcxxspglVo();
        flowVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        flowVo.setFlcmc(oaXcxxspFlowConstant.getFlowName());
        IPage<OaXcxxspglVo> flowList = oaXcxxspglFlowMapper.flowJkPage(page, onlyCurrentUser, user, flowVo);

        Map<String, Long> FlowVo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(flowList.getRecords())) {
            FlowVo = flowList.getRecords().stream().collect(Collectors.groupingBy(
                    OaXcxxspglVo::getFhjmc, Collectors.counting()
            ));
        }

        List<NodeTjVo> xcxxspMap = FlowVo.entrySet().stream().map(e -> new NodeTjVo(e.getKey(), e.getValue())).collect(Collectors.toList());
        resultMap.put("oaFlowNode", xcxxspMap);

        return resultMap;
    }

    public Map myXcxxList(boolean onlyCurrentUser, CurrentUserVo user) {
        Map map = new HashMap<>();
        OaXcxxspglVo oaXcxxspglVo = new OaXcxxspglVo();
        oaXcxxspglVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        List<OaXcxxspglVo> xcxxFlowList = oaXcxxspglFlowMapper.flowJkList(onlyCurrentUser, user, oaXcxxspglVo);
        map.put("oaFlowNode", xcxxFlowList);
        return map;
    }

    //会议室管理
    public Map myhysglFlowDb(Page<OaFlowVo> page1, boolean onlyCurrentUser, CurrentUserVo user) {
        Map resultMap = new HashMap();

        Page<OaHysglVo> page = new Page<>(1, 100);
        OaHysglVo flowVo = new OaHysglVo();
        flowVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        flowVo.setFlcmc(oaHysglFlowConstant.getFlowName());
        IPage<OaHysglVo> flowList = oaHysglFlowMapper.flowJkPage(page, onlyCurrentUser, user, flowVo);

        Map<String, Long> FlowVo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(flowList.getRecords())) {
            FlowVo = flowList.getRecords().stream().collect(Collectors.groupingBy(
                    OaHysglVo::getFhjmc, Collectors.counting()
            ));
        }

        List<NodeTjVo> hyssqglMap = FlowVo.entrySet().stream().map(e -> new NodeTjVo(e.getKey(), e.getValue())).collect(Collectors.toList());
        resultMap.put("oaFlowNode", hyssqglMap);

        return resultMap;
    }

    public Map myhysList(boolean onlyCurrentUser, CurrentUserVo user) {
        Map map = new HashMap<>();
        OaHysglVo oaHysglVo = new OaHysglVo();
        oaHysglVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        List<OaHysglVo> hysFlowList = oaHysglFlowMapper.flowJkList(onlyCurrentUser, user, oaHysglVo);
        map.put("oaFlowNode", hysFlowList);
        return map;
    }

    //办公用品申请
    public Map mybgypsplFlowDb(Page<OaFlowVo> page1, boolean onlyCurrentUser, CurrentUserVo user) {
        Map resultMap = new HashMap();

        Page<OaBgypsqglVo> page = new Page<>(1, 100);
        OaBgypsqglVo flowVo = new OaBgypsqglVo();
        flowVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        flowVo.setFlcmc(oaBgypsqglFlowConstant.getFlowName());
        IPage<OaBgypsqglVo> flowList = oaBgypsqglFlowMapper.flowJkPage(page, onlyCurrentUser, user, flowVo);

        Map<String, Long> FlowVo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(flowList.getRecords())) {
            FlowVo = flowList.getRecords().stream().collect(Collectors.groupingBy(
                    OaBgypsqglVo::getFhjmc, Collectors.counting()
            ));
        }

        List<NodeTjVo> bgspsqMap = FlowVo.entrySet().stream().map(e -> new NodeTjVo(e.getKey(), e.getValue())).collect(Collectors.toList());
        resultMap.put("oaFlowNode", bgspsqMap);

        return resultMap;
    }

    public Map mybgypList(boolean onlyCurrentUser, CurrentUserVo user) {
        Map map = new HashMap<>();
        OaBgypsqglVo oaBgypsqglVo = new OaBgypsqglVo();
        oaBgypsqglVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        List<OaBgypsqglVo> bgsypsqFlowList = oaBgypsqglFlowMapper.flowJkList(onlyCurrentUser, user, oaBgypsqglVo);
        map.put("oaFlowNode", bgsypsqFlowList);
        return map;
    }

    //公文管理-收文


    //公文管理-发文


    //加班值班
    public Map myjbzblFlowDb(Page<OaFlowVo> page1, boolean onlyCurrentUser, CurrentUserVo user) {
        Map resultMap = new HashMap();

        Page<OaJbzbVo> page = new Page<>(1, 100);
        OaJbzbVo flowVo = new OaJbzbVo();
        flowVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        flowVo.setFlcmc(oajbzbglFlowConstant.getFlowName());
        IPage<OaJbzbVo> flowList = oaJbzbFlowMapper.flowJkPage(page, onlyCurrentUser, user, flowVo);

        Map<String, Long> FlowVo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(flowList.getRecords())) {
            FlowVo = flowList.getRecords().stream().collect(Collectors.groupingBy(
                    OaJbzbVo::getFhjmc, Collectors.counting()
            ));
        }

        List<NodeTjVo> jbzbMap = FlowVo.entrySet().stream().map(e -> new NodeTjVo(e.getKey(), e.getValue())).collect(Collectors.toList());
        resultMap.put("oaFlowNode", jbzbMap);

        return resultMap;
    }

    public Map myjbzbList(boolean onlyCurrentUser, CurrentUserVo user) {
        Map map = new HashMap<>();
        OaJbzbVo oaJbzbVo = new OaJbzbVo();
        oaJbzbVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        List<OaJbzbVo> jbzbFlowList = oaJbzbFlowMapper.flowJkList(onlyCurrentUser, user, oaJbzbVo);
        map.put("oaFlowNode", jbzbFlowList);
        return map;
    }

    //请休假
    public Map myqxjFlowDb(Page<OaFlowVo> page1, boolean onlyCurrentUser, CurrentUserVo user) {
        Map resultMap = new HashMap();

        Page<OaQxjspVo> page = new Page<>(1, 100);
        OaQxjspVo flowVo = new OaQxjspVo();
        flowVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        flowVo.setFlcmc(oaqxjFlowConstant.getFlowName());
        IPage<OaQxjspVo> flowList = oaQxjspFlowMapper.flowJkPage(page, onlyCurrentUser, user, flowVo);

        Map<String, Long> FlowVo = new HashMap<>();
        if (CollectionUtils.isNotEmpty(flowList.getRecords())) {
            FlowVo = flowList.getRecords().stream().collect(Collectors.groupingBy(
                    OaQxjspVo::getFhjmc, Collectors.counting()
            ));
        }

        List<NodeTjVo> qxjMap = FlowVo.entrySet().stream().map(e -> new NodeTjVo(e.getKey(), e.getValue())).collect(Collectors.toList());
        resultMap.put("oaFlowNode", qxjMap);

        return resultMap;
    }
    public Map myqxjList(boolean onlyCurrentUser, CurrentUserVo user) {
        Map map = new HashMap<>();
        OaQxjspVo oaQxjspVo = new OaQxjspVo();
        oaQxjspVo.setFblzt(OaCommonFlowConstant.BLZT_DB);
        List<OaQxjspVo> qxjFlowList = oaQxjspFlowMapper.flowJkList(onlyCurrentUser, user, oaQxjspVo);
        map.put("oaFlowNode", qxjFlowList);
        return map;
    }







    public Result recall(String oaid) {
        List<OaProcessBusiness> oaProcessBusinesses = oaProcessBusinessService.list(new LambdaQueryWrapper<OaProcessBusiness>().eq(OaProcessBusiness::getFpid, oaid));
        OaProcessBusiness businessServiceById = oaProcessBusinesses.get(0);

        Integer fsfkch = businessServiceById.getFsfkch();
        if (fsfkch == null){
            businessServiceById.setFsfkch(0);
            oaProcessBusinessService.updateById(businessServiceById);
        }else {
            //当是否可撤回为0 的时候 就是可以撤回 什么时候变为0
            //当字段为1时 更新
            if (fsfkch == 1) {
                //上一环节id 为空   /且上一环节id为上一环节id为空的数据为待办 就 可以一直退回 一直为0 一直变成0
                //初始化提交 的时候 就已经是0 现在是提交的时候 只需要更新 业务表字段

                //第一个环节
                List<OaJobEntity> oaJobEntityList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getFpid, oaid)
                        .eq(OaJobEntity::getFblzt, OaFlowConstant.FLOW_BLZT_YB)
                        .isNull(OaJobEntity::getFsyhjid));

                //当长度大于1时 表示有退回 ，则循环查询？
                String jobId = "";
                if (oaJobEntityList.size() > 1) {
                    for (OaJobEntity job : oaJobEntityList) {
                        jobId = job.getId();
                    }

                } else {
                    jobId = oaJobEntityList.get(0).getId();
                }
                List<OaJobEntity> sqsxJobs = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getFpid, oaid)
                        .eq(OaJobEntity::getFblzt, OaFlowConstant.FLOW_BLZT_DB)
                        .eq(OaJobEntity::getFsyhjid, jobId));
                if (sqsxJobs.size() > 0) {
                    businessServiceById.setFsfkch(0);
                    oaProcessBusinessService.updateById(businessServiceById);
                } else return null;

            } else {
                //获取第一个已办环节
                String jobId = "";
                List<OaJobEntity> oaJobEntityList = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getFpid, oaid)
                        .eq(OaJobEntity::getFblzt, OaFlowConstant.FLOW_BLZT_YB)
                        .isNull(OaJobEntity::getFsyhjid));
                //当第一个已办环节不为空 的时候
                if (oaJobEntityList != null) {
                    if (oaJobEntityList.size() > 1) {
                        for (OaJobEntity job : oaJobEntityList) {
                            jobId = job.getId();
                        }

                    } else {
                        jobId = oaJobEntityList.get(0).getId();
                    }
                    List<OaJobEntity> sqsxJobs = spcySqsxJobService.list(new LambdaQueryWrapper<OaJobEntity>().eq(OaJobEntity::getFpid, oaid)
                            .eq(OaJobEntity::getFblzt, OaFlowConstant.FLOW_BLZT_YB)
                            .eq(OaJobEntity::getFsyhjid, jobId));
                    if (sqsxJobs.size() > 0) {
                        businessServiceById.setFsfkch(1);
                        oaProcessBusinessService.updateById(businessServiceById);
                    } else return null;
                }
            }
        }
        return  Result.ok("插入成功");
    }


}


























